<?php

class LoginController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout->disableLayout();
    }

    public function indexAction()
    {
    	$username = $this->getRequest()->getParam("username");
    	$password = $this->getRequest()->getParam("password");
    	
    	if(!$username || !$password){
    		return;
    	}
    	
        $db = Zend_Registry::get("db");
        $authAdapter = new Zend_Auth_Adapter_DbTable($db);
        $authAdapter->setTableName('users');
        $authAdapter->setIdentityColumn('username');
        $authAdapter->setCredentialColumn('password');
        
        $authAdapter->setIdentity($username);
        $authAdapter->setCredential(md5($password));
        
        $auth = Zend_Auth::getInstance();
        $res = $auth->authenticate($authAdapter);
        if (!$res->isValid()) { 
        	$this->view->shake = TRUE;     	
        	$this->view->poruka = $res->getMessages();
        }
        else{
        	$this->_redirect("/admin/index");
        }
    }
    
    public function logoutAction()
    {
    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity();
    	$this->_forward("index");
    }


}

